package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/2/26 19:26
 * @description
 */
public class Code926 {
    /*
     * 某一位为1时，前面一位是0或者1都可以
     * 某一位为0时，前面一位只能为0
     */
    public int minFlipsMonoIncr(String S) {
        int n = S.length();
        if (n < 2) { return 0; }
        int zero = 0, one = 0;
        for (int i = 0; i < n; i++) {
            if (S.charAt(i) == '1') {
                one = Math.min(zero, one);
                zero += 1;
            } else {
                one = Math.min(zero, one) + 1;
            }
        }
        return Math.min(zero, one);
    }
}
